package com.summer.order.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.summer.core.annotations.ScopeType;
import com.summer.core.controller.EntityQuery;
import com.summer.core.controller.LoginUserInfo;
import com.summer.core.util.ArithUtil;
import com.summer.core.util.FacesUtil;
import com.summer.order.model.Cookbook;
import com.summer.order.model.DayFood;
import com.summer.order.model.UserDayFood;
import com.summer.order.model.UserOrder;
import com.summer.order.util.OrderConstants;
import com.summer.user.model.User;

@Component
@Scope(ScopeType.VIEW)
public class UserOrderDayFoodList extends EntityQuery<DayFood>{

	
	@Resource
	LoginUserInfo loginUserInfo;
	
	private Cookbook cookbook = new Cookbook();
	
	private List<DayFood> orderDayFoodList;
	
	
	public final String hql = "select df from DayFood df";
	public final String count_hql = "select count(df) from DayFood df";
	private List<UserDayFood> userDayFoods;
	public UserOrderDayFoodList() {
		
		final String[] RESTRICTIONS = {
				"df.cookbook.id = #{userOrderDayFoodList.cookbook.id}"
		};
		
		setHql(hql);
		setCountHql(count_hql);
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
	}
	
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void saveOrder() {
		
		if(orderDayFoodList == null || orderDayFoodList.size() == 0) {
			FacesUtil.addErrorMessage("你这一天也没选，订哪些啊？");
		}
		
		String loginUserId = loginUserInfo.getLoginUserId();
		User user = getHt().get(User.class, loginUserId);
		UserOrder userOrder = new UserOrder();
		userOrder.setId(orderDayFoodList.get(0).getCookbook().getId()+user.getId());
		
		@SuppressWarnings("unchecked")
		List<UserOrder> list = getHt().find("from UserOrder uo where uo.id=? and uo.user.id=?",userOrder.getId(),user.getId());
		if(list != null && list.size() > 0) {
			FacesUtil.addErrorMessage("你这周的都订过了，别再订了哦～～～");
		}
		userOrder.setUser(user);
		userOrder.setCookbook_id(orderDayFoodList.get(0).getCookbook().getId());
		userDayFoods = new ArrayList<UserDayFood>();
		if(orderDayFoodList != null) {
			for (DayFood dayFood : orderDayFoodList) {
				if(dayFood.getBreakfast() == false && dayFood.getLunch() == false) {
					continue;
				}
				UserDayFood udf = new UserDayFood();
				udf.setBreakfast(dayFood.getBreakfast() == true ? "yes" : "no");
				udf.setLunch(dayFood.getLunch() == true ? "yes" : "no");
				udf.setDate(dayFood.getDate());
				udf.setId(dayFood.getId()+user.getId());
				udf.setWeekday(dayFood.getWeekday());
				udf.setCookbook(dayFood.getCookbook());
				userDayFoods.add(udf);
			}
		}
		
		Double money = 0.0;
		userOrder.setOrderDayCount(userDayFoods.size());
		for (UserDayFood udf : userDayFoods) {
			udf.setUserOrder(userOrder);
			if(udf.getBreakfast().equals("yes")) {
				money = ArithUtil.add(money,3D);
			}
			if(udf.getLunch().equals("yes")) {
				money = ArithUtil.add(money,6D);
			}
		}
		userOrder.setMoney(money);
		userOrder.setDateBetween(orderDayFoodList.get(0).getCookbook().getDateBetween());
		userOrder.setUserDayFoods(new HashSet<UserDayFood>(userDayFoods));
		getHt().save(userOrder);
		
		FacesUtil.addInfoMessage("订完了，准备钱儿吧");
		System.out.println(FacesUtil.getContextPath());
		FacesUtil.sendRedirect(FacesUtil.getContextPath()+"/admin/order/myOrderList.htm");
		
	}
	
	public boolean checkCookbook(String cookbook_id) {
		boolean b = false;
		if(cookbook_id != null && !cookbook_id.equals("")) {
			Cookbook cookbook = getHt().get(Cookbook.class, cookbook_id);
			if(cookbook != null && cookbook.getStatus() == OrderConstants.CookbookStatus.ENABLE) {
				b = true;
			}
		}
		
		return b;
	}
	
	@Override
	public DayFood getLazyModelRowData(String rowKey) {
		List<DayFood> dayFoodList = (List<DayFood>) getLazyModel().getWrappedData();
		for (DayFood dayFood : dayFoodList) {
			if (dayFood.getId().equals(rowKey)) {
				return dayFood;
			}
		}
		return null;
	}

	@Override
	public Object getLazyModelRowKey(DayFood dayFood) {
		return dayFood.getId();
	}
	


	
	public Cookbook getCookbook() {
		return cookbook;
	}

	public void setCookbook(Cookbook cookbook) {
		this.cookbook = cookbook;
	}

	public List<DayFood> getOrderDayFoodList() {
		
		return orderDayFoodList;
	}

	public void setOrderDayFoodList(List<DayFood> orderDayFoodList) {
		this.orderDayFoodList = orderDayFoodList;
	}



	public List<UserDayFood> getUserDayFoods() {
		
		
		return userDayFoods;
	}



	public void setUserDayFoods(List<UserDayFood> userDayFoods) {
		this.userDayFoods = userDayFoods;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
